﻿#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using OfficeOpenXml;

public class GetLabelText : ScriptableObject
{
	static List<ExeclDate> AllExeclDate = new List<ExeclDate>();

	[MenuItem ("AiFeng/Get All Label Text")]
	static void GetAllLabelText ()
	{
		AllExeclDate.Clear ();
		GetScriptsText ();
		GetSceneLabelText ();
		GetAssetsLabelText ();
		WriteXLSX ();
		AssetDatabase.Refresh ();
	}

	static void GetScriptsText ()
	{
		string[] filePaths1 = null;
		string[] filePaths2 = null;
		string[] filePaths3 = null;
		string[] filePaths4 = null;
		try
		{
			filePaths1 = Directory.GetFiles(Application.dataPath, "*.cs", SearchOption.AllDirectories);
			filePaths2 = Directory.GetFiles(Application.dataPath, "*.json", SearchOption.AllDirectories);
			filePaths3 = Directory.GetFiles(Application.dataPath, "*.txt", SearchOption.AllDirectories);
			filePaths4 = Directory.GetFiles(Application.dataPath, "*.xml", SearchOption.AllDirectories);
		}
		catch (System.Exception error)
		{
			Debug.Log(error.Message);
		}
		List<string> filePaths = new List<string> ();
		foreach (string path in filePaths1)
		{
			filePaths.Add(path);
		}
		foreach (string path in filePaths2)
		{
			filePaths.Add(path);
		}
		foreach (string path in filePaths3)
		{
			filePaths.Add(path);
		}
		foreach (string path in filePaths4)
		{
			filePaths.Add(path);
		}
		foreach (string path in filePaths)
		{
			if (!path.Contains("Resources") && !path.Contains("ResourcesAsset") && !path.Contains("Script") && !path.Contains("StreamingAssets"))
			{
				continue;
			}
			if (path.Contains("NGUI") || path.Contains("TK2DROOT") || path.Contains("texiao_scene"))
			{
				continue;
			}
			int lastIndex = path.LastIndexOf("/");
			string FilePath = path.Remove(0, lastIndex + 1);
//			Debug.Log(FilePath);
			string[] allLines = File.ReadAllLines(FilePath);
			for (int i = 0; i < allLines.Length; i ++)
			{
				string ThisLine = allLines [i];
				if (ThisLine.Contains ("///") || ThisLine.Contains ("region") || ThisLine.Contains ("endregion") || 
				    ThisLine.Contains ("Debug.Log") || ThisLine.Contains ("/*") || ThisLine.Contains ("*/"))
				{
					continue;
				}
				bool haschinese = false;
				char[] allChar = ThisLine.ToCharArray();
				foreach (char ch in allChar)
				{
					if (ch >= 0x4e00 && ch <= 0x9fa5) 
					{
						haschinese = true;
						break;
					}
				}
				if (!haschinese)
				{
					continue;
				}
				if (ThisLine.Contains ("//"))
				{
					int index = ThisLine.IndexOf ("//");
					if (index == 0)
					{
						continue;
					}
					string Left = ThisLine.Substring (0, index);
					if (!string.IsNullOrEmpty(Left))
					{
						char[] mChar = Left.ToCharArray();
						foreach (char ch in mChar)
						{
							if (ch >= 0x4e00 && ch <= 0x9fa5) 
							{
								ExeclDate date = new ExeclDate();
								date.cell1 = path;
								date.cell2 = "第" + (i + 1).ToString() + "行";
								date.cell3 = Left;
								AllExeclDate.Add(date);
								break;
							}
						}
					}
				}
				else
				{
					ExeclDate date = new ExeclDate();
					date.cell1 = path;
					date.cell2 = "第" + (i + 1).ToString() + "行";
					date.cell3 = ThisLine;
					AllExeclDate.Add(date);
				}
			}
		}
	}

	static void GetSceneLabelText ()
	{
		GameObject[] AllSceneObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject));
		List<GameObject> AllObjects = new List<GameObject>();
		foreach (GameObject Obj in AllSceneObjects)
		{
			if (Obj == null)
			{
				continue;
			}
			if (Obj.hideFlags == HideFlags.NotEditable)
			{
				continue;
			}
			if (Obj.hideFlags == HideFlags.HideAndDontSave)
			{
				continue;
			}
			AllObjects.Add(Obj);
		}
		foreach (GameObject value in AllObjects)
		{
			UILabel[] labels = value.GetComponentsInChildren<UILabel>(true);
			if (labels.Length == 0)
			{
				continue;
			}
			foreach (UILabel eachLabel in labels)
			{
				if (string.IsNullOrEmpty(eachLabel.text))
				{
					continue;
				}
				char[] allChar = eachLabel.text.ToCharArray();
				foreach (char ch in allChar)
				{
					if (ch >= 0x4e00 && ch <= 0x9fa5) 
					{
						ExeclDate date = new ExeclDate();
						date.cell1 = value.name;
						date.cell2 = eachLabel.name;
						date.cell3 = eachLabel.text;
						AllExeclDate.Add(date);
						break;
					}
				}
			}
		}
	}

	static void GetAssetsLabelText ()
	{
		string[] allpaths = AssetDatabase.GetAllAssetPaths ();
		foreach (string path in allpaths)
		{
			if (!path.Contains("Assets/Resources/") && !path.Contains("Assets/ResourcesAsset/"))
			{
				continue;
			}
			if (path.Contains("AssetsVN/") || path.Contains("Assets/ResourcesVN/") || path.Contains("Assets/ResourcesAssetVN/"))
			{
				continue;
			}
			Object obj = AssetDatabase.LoadMainAssetAtPath(path);
			if (obj == null)
			{
				continue;
			}
			GameObject go = obj as GameObject;
			if (go == null)
			{
				continue;
			}
			UILabel[] labels = go.GetComponentsInChildren<UILabel>(true);
			if (labels.Length == 0)
			{
				continue;
			}
			foreach (UILabel eachLabel in labels)
			{
				if (string.IsNullOrEmpty(eachLabel.text))
				{
					continue;
				}
				char[] allChar = eachLabel.text.ToCharArray();
				foreach (char ch in allChar)
				{
					if (ch >= 0x4e00 && ch <= 0x9fa5) 
					{
						ExeclDate date = new ExeclDate();
						date.cell1 = path;
						date.cell2 = eachLabel.name;
						date.cell3 = eachLabel.text;
						AllExeclDate.Add(date);
						break;
					}
				}
			}
		}
	}

	//写入xlsx
	static void WriteXLSX ()
	{
		string path = Application.dataPath.Replace("Assets", "AssetsVN") + "/翻译表（客户端）.xlsx";
		FileStream fs = new FileStream(path, FileMode.Create);
		using (ExcelPackage package = new ExcelPackage(fs))
		{
			ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("客户端");
			for (int i = 0; i < AllExeclDate.Count; i ++)
			{
				worksheet.Cells[i + 1, 1].Value = AllExeclDate[i].cell1;
				worksheet.Cells[i + 1, 2].Value = AllExeclDate[i].cell2;
				worksheet.Cells[i + 1, 3].Value = AllExeclDate[i].cell3;
			}
			package.Save();
		}
		fs.Close();
	}
}

public class ExeclDate 
{
	public string cell1 = "";
	public string cell2 = "";
	public string cell3 = "";
}
#endif